//
//  checkboxButton.m
//  jiayuan
//
//  Created by xuej on 3/14/11.
//  Copyright 2011 xsoft. All rights reserved.
//

#import "CheckboxButton.h"


@implementation CheckboxButton
@synthesize caption;
@synthesize uncheckedImage;
@synthesize checkedImage;
@synthesize imageSize;
// @synthesize checked;
@synthesize fontSize;




- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        fontSize = 14.0;
        
    }
    return self;
}

//- (void)awakeFromNib
//{
//    [super awakeFromNib];
//}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        fontSize = 14.0;
        
        // add by herui 2014.9.30 提供ARC支持
//        [self addObserver:self
//               forKeyPath:@"checked"
//                  options:NSKeyValueObservingOptionNew |
//         NSKeyValueObservingOptionOld
//                  context:nil];
    }
    return self;
}

- (void)setChecked:(Boolean)checked
{
    if (_checked == checked) {
        return;
    }
    _checked = checked;
    [self setNeedsDisplay];
    
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
	UIImage *image = nil;
	
	if (_checked) {
		image = [UIImage imageNamed:checkedImage];
	} else {
		image = [UIImage imageNamed:uncheckedImage];
	}
	
    // Image Rect
	CGRect rcFrame = CGRectMake(15.0f, (rect.size.height - imageSize.height)/2.0f, imageSize.width, imageSize.height);
	[image drawInRect:rcFrame];
    
    // font Color
    UIColor *fontColor = UIColorFromRGB(0x5d5d5d);
    [fontColor set];
	if (fontSize < 1.0) {
        fontSize = 15.0;
    }
    
	UIFont *captionFont = [UIFont systemFontOfSize:fontSize];
	if (rect.size.height == 33) {
		[caption drawAtPoint:CGPointMake(imageSize.width + 6, 6.0f) withFont:captionFont];
	} else if (rect.size.height == 37) {
		[caption drawAtPoint:CGPointMake(imageSize.width + 6, 9.0f) withFont:captionFont];
	}else {
        [caption drawAtPoint:CGPointMake(15.0f+imageSize.width+10, 9.0f) withFont:captionFont];
    }
}



@end
